Go-Golang Lanuage Overview

A networking backend language designed for concurrency

Created: July 29th 2019
Updated: December 7th 2019
Connect with us on
image
image

Language Properties

  • Typing : Statically Typed, Strongly typed
  • Paradigms: Multiparadigm; Imperative, Concurrent, Object oriented, Functional, etc.
  • Created: November 10 2009
  • Used by: Uber, Google, Twitch, Medium, etc.

Useful Starter Resources

A list of first party documentation

A tour of go (official interactive tour of the language)

Official Go (also called golang) Website

Go source code

Go standard library source monorepo

Effective Go (A sort of style/best practices guide)

Comments/Documentation

The commenting system in Go is pretty standard, below are examples of single and multiline comments.

// This is a single line comment

/**
This
is
a
multiline
comment
*/

Unlike python there is no explicit syntax for docstrings in go.

Variables & Typing

Go is a statically typed and strongly typed language; This means that you must declare variables types (int, string etc.) when you initialize variables, and that variable types cannot be changed.

There are three syntactically distinct ways to create variables in Go:

The first is to instantiate with an explicit type and then assign a value

var greeting string
greeting = "hello"

The second is to instantiate and assign a value all at once

var greeting string = "hello"

The third is somewhat confusing and leads people to believe that Go is dynamically typed. The third can ONLY be used in functions and allows you to instantiate a variable without a specific type being DECLARED, the type is inferred from what you provide.

func greet(name string){
    greeting := "Hello"
    fmt.Println(greeting, name) // prints Hello <Name>
}

Functions

Below are examples of function declarations in Go, there are examples of functions with and without arguments:

// An example of a function with no arguments provided
func hello_world(){
    fmt.Println("Hello World!")
}

// An example of a function that takes two arguments and prints them, remember you MUST specify argument type for each argument
func greet(name string, greeting string) {
    fmt.Println(greeting, name)
}

Running Go Code

To run go code there are two options either:

Run go build <filename> then run the resulting .exe(windows)/binary(mac and linux) file.

Run go run <filename>, this will run the code by compiling and running the result but it does not save the resulting .exe(windows)/binary(mac and linux) file.

Files

basicsDemo.go

// Allows the file to be run by itself after compiling, SEE: https://golang.org/doc/code.html#PackageNames
package main

//import statements are similar to python syntax
import "fmt"

// This is the 'full' syntax for declaring a variable, NOTE: because go is statically typed a type MUST be provided
var greeting string = "Hello,"

// In go a main function is automatically called.
func main() {
  /**This is the shortform syntax for declaring a variable. It infers the type automatically, but still
  adheres to the precepts of static typing; NO implicit type casting.
  NOTE: This only works INSIDE functions, you cannot define global scope variables this way*/
  name := "John Doe"
  greet(name, greeting)
}

// A simple function declaration that takes two parameters and prints them
func greet(name string, greeting string) {
  fmt.Println(greeting, name)
}

Thank you for your support!

Be sure to share the post if it was helpful!